package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"shops/models"

	"shops/shoprpc/internal/svc"
	"shops/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ForgetpasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewForgetpasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ForgetpasswordLogic {
	return &ForgetpasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ForgetpasswordLogic) Forgetpassword(in *shoprpc.ForgetpasswordRequest) (*shoprpc.ForgetpasswordResponse, error) {
	// todo: add your logic here and delete this line
	if in.Password != in.Newpassword {
		return nil, errors.New("两次密码输入不一致，请重新输入")

	}
	var findUser models.User
	if findUser.Password == in.Password {
		return nil, errors.New("新密码不可与原密码一致")
	}
	findUser = models.User{
		Model:    gorm.Model{},
		Account:  in.Account,
		Password: in.Password,
	}
	models.DB.Debug().Updates(&findUser)
	return &shoprpc.ForgetpasswordResponse{
		UserId: int64(findUser.ID),
	}, nil
}
